package plus.chendd.jdk8;

import org.junit.Test;
import plus.chendd.service.PersonService;
import plus.chendd.service.PersonServiceImpl;
import plus.chendd.service.UserService;
import plus.chendd.entity.User;

import java.util.ArrayList;
import java.util.Collections;

public class LambdaDemo {
  @Test
  public void startTest001() {
    new Thread(new Runnable() {
      @Override
      public void run() {
        System.out.println("sub thread execute code");
      }
    }).start();
    
    System.out.println("main thread execute code");
  }
  
  public void callShow(UserService userService) {
    userService.show();
  }
  
  @Test
  public void startTest002() {
    callShow(() -> {
      System.out.println("show method execute code");
    });
  }
  
  
  @Test
  public void startTest003() {
    ArrayList<User> users = new ArrayList<User>() {{
      add(new User("zhangsna", 18));
      add(new User("lisi", 22));
      add(new User("wanwu", 20));
    }};
    
    Collections.sort(users, (o1, o2) -> o1.getAge() - o2.getAge());
    users.stream().forEach(System.out::println);
  }
  
  @Test
  public void startTest004() {
    PersonServiceImpl personService = new PersonServiceImpl();
    personService.sleep();
    personService.show();
    
    // 此时是调用静态方法。可以通过接口直接调用
    PersonService.eat();
  }
}
